﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{

    public class TriggerListCompare : ICompareAbleListCompare<TriggerInfo, TriggerCompareResult>
    {
        /// <summary>
        /// 触发器的比较类.
        /// </summary>
        private ICompareAbleCompare<TriggerInfo, TriggerCompareResult> triggerCompare = new TriggerCompare();





        public List<TriggerCompareResult> Compare(List<TriggerInfo> leftTriggerInfoList, List<TriggerInfo> rightTriggerInfoList)
        {
            // 结果表表.
            List<TriggerCompareResult> resultList = new List<TriggerCompareResult>();

            // 为避免下面的 Linq 处理异常
            // 假如 一个表的  表 的表表为 null
            // 将其设置为一个 空的表表.
            if (leftTriggerInfoList == null)
            {
                leftTriggerInfoList = new List<TriggerInfo>();
            }
            if (rightTriggerInfoList == null)
            {
                rightTriggerInfoList = new List<TriggerInfo>();
            }


            // 使用 Linq 
            // 检索两个表的 表 的并集。
            // 同时合并掉 相同的表.
            var query =
                (from trigger in leftTriggerInfoList
                 select trigger.Name
                    ).Union
                (from trigger in rightTriggerInfoList
                 select trigger.Name
                    );


            // 获取 表名 List.
            List<string> triggerNameList = query.ToList();

            // 表名排序.
            triggerNameList.Sort();


            // 依次比较表.
            for (int i = 0; i < triggerNameList.Count; i++)
            {
                // 检索左边的表.
                var queryLeft =
                    from data in leftTriggerInfoList
                    where data.Name == triggerNameList[i]
                    select data;

                // 检索右边的表.
                var queryRight =
                    from data in rightTriggerInfoList
                    where data.Name == triggerNameList[i]
                    select data;


                // 表的比较结果.
                TriggerCompareResult triggerCompareResult = null;

                if (queryLeft.Count() == 0)
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找不到表， 右表也找不到表.
                        // 理论上这种情况不会发生.
                        triggerCompareResult = triggerCompare.Compare(null, null);
                    }
                    else
                    {
                        // 左表找不到表， 右表找到表.
                        triggerCompareResult = triggerCompare.Compare(null, queryRight.First());
                    }
                }
                else
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找到表， 右表找不到表.
                        triggerCompareResult = triggerCompare.Compare(queryLeft.First(), null);
                    }
                    else
                    {
                        // 左表找到表， 右表找到表.
                        triggerCompareResult = triggerCompare.Compare(queryLeft.First(), queryRight.First());
                    }
                }


                // 将 表的比较结果加入最后的比较表表.
                resultList.Add(triggerCompareResult);

            }

            return resultList;
        }
    }
}
